package com.lv.servlet;

import com.lv.bean.Station;
import com.lv.dao.StationDao;
import com.lv.dao.StationDaoImpl;
import org.codehaus.jackson.map.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class GetStationServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String area = request.getParameter("area");
        System.out.println(area);
        StationDao stationDao = new StationDaoImpl();
        if (area!=null&&!"".equals(area)){
            System.out.println(">>>>>>>>>>>>>>>");
            response.setCharacterEncoding("UTF-8");
            List<Station> stations = stationDao.selectStationOfCityId(area);
            ObjectMapper mapper = new ObjectMapper();
            String result = mapper.writeValueAsString(stations);
            response.setContentType("text/javascript");
            response.getWriter().print(result);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
